$(function() {
	reloadList();
	$('#mobileAddBtn').on('click',function() {
		var strArr = [];
		strArr.push('<div class="control-group">');
			strArr.push('<label class="control-label">&nbsp;</label>');
			strArr.push('<div class="controls control-row-auto">');
				strArr.push('<input type="number" name="mobile" style="width:140px;"/>&nbsp;');
				strArr.push('<a href="#" onclick="delCurrentElementGroup(this);">删除</a>');
			strArr.push('</div>');
		strArr.push('</div>');
		
		$('#addNewForm').children(':eq(0)').append(strArr.join(''));
	});
});

function delCurrentElementGroup(event) {
	var _controlGroupDiv = $(event).parent().parent();
	_controlGroupDiv.empty().remove();
}

BUI.use('bui/toolbar', function(Toolbar) {
	var bar = new Toolbar.Bar({
		render : '#tool-bar',
		elCls : 'toolbar',
		children : [ {
			xtype : 'button',
			btnCls : 'button button-primary',
			text : '新建短信'
		} ],
		handler : function() {
			resetAddNewForm();
			dialog.show();
		}
	});
	bar.render();
});

var dialog;
BUI.use([ 'bui/overlay', 'bui/form' ], function(Overlay, Form) {
	var form = new Form.Form({
		srcNode : '#addNewForm'
	}).render();

	dialog = new Overlay.Dialog({
		title : '短信发送',
		width : 400,
		height : 'auto',
		closeable : false,
		contentId : 'addNewContent',
		mask : true,
		buttons : [ {
			text : '发送',
			elCls : 'button button-primary',
			handler : function() {
				sendNewSms(this);
			}
		}, {
			text : '取消',
			elCls : 'button',
			handler : function() {
				this.close();
			}
		} ]
	});

});

function resetAddNewForm() {
	$('#addNewForm').find('[name="smsContent"]').removeAttr("readonly");
	$('#addNewForm').find('[name="sameOperateNo"]').val('');
	$('#addNewForm').get(0).reset();
	
	delOtherMobileInput();
}

function delOtherMobileInput() {
	$('#mobile-input-group').nextAll().remove();
}

function sendNewSms(_context) {
	$.ajax({
		type : "POST",
		url : "../../message/sms/batchSend",
		data : serializeForm('#addNewForm'),
		contentType : "application/json",
		success : function(data) {
			if (data && data == 'success') {
				// 重新加载数据
				reloadList();
				alertMsg(1, '发送成功!');
			} else {
				// 重新加载数据
				reloadList();
				alertMsg(0, '发送失败!');
			}
		},
		error : function(data) {
			// 重新加载数据
			reloadList();
			alertMsg(0, '发送失败!');
		}
	});

	_context.close();
}

function loadFormDataAndShowDialog(_event) {
	resetAddNewForm();
	
	var currentTr = $(_event.currentTarget).parent().parent();
	var sameOperateNo = currentTr.children(':eq(0)').get(0).innerText;
	var smsContent = currentTr.children(':eq(1)').get(0).innerText;
	var unDoneCount = currentTr.children(':eq(3)').get(0).innerText;
	var num = 100000;
	if (unDoneCount < 100000)
		num = unDoneCount;

	// 回显
	$('#addNewForm').find('[name="sameOperateNo"]').val(sameOperateNo);
	$('#addNewForm').find('[name="smsContent"]').val(smsContent);
	$('#addNewForm').find('[name="smsContent"]').attr("readonly", "readonly");
	$('#addNewForm').find('[name="limit"]').val(num);
	
	dialog.show();
}

function reloadList() {
	$('.smsTableDiv tbody').empty();
	$.ajax({
		type : "POST",
		url : "../../message/sms/list",
		dataType : 'json',
		success : function(data) {
			if (data && data.length > 0) {
				var htmlArr = [];
				for ( var i in data) {
					htmlArr.push('<tr>');
					htmlArr.push('<td>');
					htmlArr.push(data[i].sameOperateNo);
					htmlArr.push('</td>');
					htmlArr.push('<td>');
					htmlArr.push(data[i].smsContent);
					htmlArr.push('</td>');
					htmlArr.push('<td>');
					htmlArr.push(data[i].doneCount);
					htmlArr.push('</td>');
					htmlArr.push('<td>');
					htmlArr.push(data[i].unDoneCount);
					htmlArr.push('</td>');
					htmlArr.push('<td>');
					htmlArr.push(formatStatus(data[i].status));
					htmlArr.push('</td>');
					htmlArr.push('<td>');
					htmlArr.push(formatOperate(data[i].status));
					htmlArr.push('</td>');
					htmlArr.push('</tr>');
				}

				$('.smsTableDiv tbody').append(htmlArr.join(""));

				$('.continueSendBtn').on('click', function(event) {
					loadFormDataAndShowDialog(event);
				});
			}
		},
		error : function(data) {
			alertMsg(0, '请求列表失败!');
		}
	});
}

function formatStatus(status) {
	if (status == 1)
		return '<span style="color:#03A159;">已完结</span>';
	return '<span style="color:#0684CA;">未完结</span>';
}

function formatOperate(status) {
	if (status == 1)
		return '<button class="button button-active button-small" disabled>发送完成</button>';
	return '<button class="button button-info button-small continueSendBtn">继续发送</button>';
}